package org.gruposp2p.dnie.client.ui.dialogs;

import org.gruposp2p.dnie.client.ui.panel.MainPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

public class ValidationErrorDialogBox extends DialogBox {

    public ValidationErrorDialogBox () {
    ensureDebugId("ValidationErrorDialogBox");
    this.setPopupPosition(
            (RootPanel.get().getOffsetWidth() -this.getOffsetWidth() )/2,
            (RootPanel.get().getOffsetHeight()-this.getOffsetHeight())/2
        );
    showDialog();
    }

    private void showDialog () {
            setText(MainPanel.constants.ValidationErrorDialogBoxCaption());
            FlexTable layout = new FlexTable();
            layout.clear();
        layout.setCellSpacing(6);
        layout.setWidth("250px");
        FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
        HTML errorMessage = new HTML(
                    MainPanel.constants.ValidationErrorMessage());
        layout.setWidget(0, 0, errorMessage);
        Button closeButton = new Button(
                    MainPanel.constants.closeButtonText(),
                    new ClickHandler() {
                      public void onClick(ClickEvent event) {
                        hide();
                      }
                    });
        HorizontalPanel buttonPanel = new HorizontalPanel();
        buttonPanel.setSpacing(5);
        buttonPanel.add(closeButton);
        buttonPanel.setWidth("250px");
        buttonPanel.setCellHorizontalAlignment(closeButton, HorizontalPanel.ALIGN_RIGHT);
        layout.setWidget(1, 0, buttonPanel);
        setWidget(layout);
        //To solve Firefox strange effect
        closeButton.setFocus(true);
    }

}